package com.ghh.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ghh.dao.OrderSettingDao;
import com.ghh.pojo.OrderSetting;
import com.ghh.service.OrderSettingService;
import com.ghh.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    //抽取的添加预约设置的方法
    private void addData(OrderSetting orderSetting){
        //判断当前日期是否已经进行了设置
        long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if(count > 0){
            //如果已经设置，执行更新操作
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else{
            //如果没有设置，执行插入操作
            orderSettingDao.add(orderSetting);
        }
    }

    //批量导入预约设置信息
    @Override
    public void addOrderSetting(List<OrderSetting> list) {
        if (list !=null&&list.size()>0){
            for (OrderSetting orderSetting : list) {
                this.addData(orderSetting);
            }
        }
    }

    //查询所有预约信息
    @Override
    public List<Map<String,Object>> getOrderSettingByMonth(String date) {
        return orderSettingDao.getOrderSettingByMonth(date);
    }

    //添加或修改单独一天的预约设置
    @Override
    public void setOrdersetting(OrderSetting orderSetting) {
        this.addData(orderSetting);
    }

    @Override
    public Double findByOrderDate(Date orderDate){
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(orderDate);
        //可预约人数
        Double number = (double) orderSetting.getNumber();
        //已预约人数
        Double reservations = (double)orderSetting.getReservations();
        //计算出已预约人数与可预约人数的百分比
        return (reservations/number)*100;
    }
}
